/*
 * configparser.c
 *
 *  Created on: Jun 24, 2013
 *      Author: koldar
 */


#include "lex-tools.h"
#include "symbols.h"
#include "lexer.h"
#include "parser-tools.h"
#include "configparser.h"
#include <stdlib.h>

char* paramToSet;

void algopt_configparser_program(PalgConfiguration config);
void algopt_configparser_stat(PalgConfiguration config);
void algopt_configparser_lvalue(PalgConfiguration config);
void updateConfigurationStruct(PalgConfiguration config,char* str,LexVal value);

void algopt_configparser_program(PalgConfiguration config){
	do {
		algopt_configparser_stat(config);
		match(SEMICOLON);
	}while (lookahead==_STRINGCONST);
}

void algopt_configparser_stat(PalgConfiguration config){
	LexVal n=match(_STRINGCONST);
	match(COLON);
	paramToSet=truncateQuotes(n.sval);
	algopt_configparser_lvalue(config);
	free(paramToSet);
}

void algopt_configparser_lvalue(PalgConfiguration config){
	switch (lookahead){
	case _STRINGCONST: {
		match(_STRINGCONST);
		break;
	}
	case _INTEGERCONST: {
		match(_INTEGERCONST);
		break;
	}
	case _BOOLEANCONST: {
		match(_BOOLEANCONST);
		break;
	}
	}
	updateConfigurationStruct(config,paramToSet,lexval);
}

void updateConfigurationStruct(PalgConfiguration config,char* str,LexVal value){
	if (strcmp(str,PATH_GRAPH)==0){
		config->graphPath=truncateQuotes(value.sval);
		return;
	}
	if (strcmp(str,VEHICLE_NUMBER)==0){
		config->vehicleNumber=value.ival;
		return;
	}
	if (strcmp(str,PATH_CONSTRUCTIVE_BMP)==0){
		config->constructiveDrawingPath=truncateQuotes(value.sval);
		return;
	}
	if (strcmp(str,DRAW_STM_STEP)==0){
		config->drawSolutionBMP=value.ival;
		return;
	}
	if (strcmp(str,OCTAVE_STM_STEP)==0){
		config->drawSolutionOctave=value.ival;
		return;
	}
	if (strcmp(str,EVALUATE_MODE)==0){
		config->evaluate=value.ival;
		return;
	}
	if (strcmp(str,NUMBER_MAX_LOS_ITERATION)==0){
		config->maximumLOSIterationNumber=value.ival;
		return;
	}
	if (strcmp(str,NUMBER_MAX_LOS_WORSE_ITERATION)==0){
		config->maximumLOSWorseningIterationNumber=value.ival;
		return;
	}
	if (strcmp(str,NUMBER_MAX_STM_ITERATION)==0){
		config->maximumSTMIterationNumber=value.ival;
		return;
	}
	if (strcmp(str,ADD_TENURE)==0){
		config->addEdgeTenure=value.ival;
		return;
	}
	if (strcmp(str,REMOVE_TENURE)==0){
		config->removeEdgeTenure=value.ival;
		return;
	}
	fprintf(stderr,"variabile \"%s\" non impostabile.\n",str);
}

PalgConfiguration parseConfigurationFile(FILE* f){
	PalgConfiguration result=initAlgConfiguration();
	yyin=f;
	next();
	algopt_configparser_program(result);
	return result;
}
